package com.example.test.util;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author linwenf
 * @Date 2020/7/9 0009 13:38
 */
public class IpUtils {
	/**
	 * 获取项目ip和名称
	 * @param req
	 * @return
	 */
	public static String getContextPath(HttpServletRequest req){
		return req.getScheme()+"://"+ req.getServerName() +":"+req.getServerPort()+req.getContextPath();
	}

	public static String  getServerName(HttpServletRequest req) {
		return req.getScheme()+"://"+ req.getServerName()+":"+req.getServerPort();
	}

	public  static  String getProjectRealPath (HttpServletRequest req){
		return  req.getSession().getServletContext().getRealPath("");
	}
	/**
	 * 获取登录用户IP地址
	 *
	 * @param request
	 * @return
	 */
	public static String getIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		if (ip.equals("0:0:0:0:0:0:0:1")) {
			ip = "本地";
		}
		return ip;
	}

}
